package com.horoscope.astrology.zodiac.signs.free.fragment

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.ProgressBar
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.activity.MainActivity
import com.horoscope.astrology.zodiac.signs.free.activity.PreMainActivity
import com.horoscope.astrology.zodiac.signs.free.bean.BaseBean
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.AnimationManager
import com.horoscope.astrology.zodiac.signs.free.utils.FrameAnimationUtils
import com.horoscope.astrology.zodiac.signs.free.utils.Logger
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import kotlinx.android.synthetic.main.fragment_load.*

/**
 * 注册等待页
 * */
class LoadFragment : BaseFragment() {

    private var percent: TextView? = null
    private var process: ProgressBar? = null
    private lateinit var anim: ValueAnimator
//    private lateinit var animationDrawable: AnimationDrawable


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_load, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView(view)
        //animationDrawable = image_view_frame?.background as AnimationDrawable
        inVisibilityRoot()

    }

    private var name = ""

    @SuppressLint("SetTextI18n")
    fun setName(name: String) {
        this.name = name
        setGuideContent(
            text_view_content_birthday!!,
            "Stranger",
            getString(R.string.tip_report_processed)
        )
        //text_view_content_birthday?.text = name + "! " + getString(R.string.tip_report_processed)
    }

    @SuppressLint("SetTextI18n")
    fun startAnim() {
        saveUserBean()
//        AppSettingManager.readObject("user_info")?.let { postUser(it) }
        FrameAnimationUtils.startBall(image_view_frame)
        anim = ValueAnimator.ofInt(100)
        anim.duration = 3000
        anim.interpolator = AccelerateDecelerateInterpolator()
        anim.addUpdateListener { animation ->
            val value = animation?.animatedValue as Int
            percent?.text = "$value%"
            process?.progress = value
            if (value == 100) {
                process?.postDelayed({

                    activity.startActivity(Intent(activity, MainActivity::class.java))
                    activity.finish()

                }, 800)

            }
        }

        anim.start()

    }

    override fun onDestroy() {
        super.onDestroy()
        if (AnimationManager.getInstance().getmDisposable() != null) {
            AnimationManager.getInstance().getmDisposable().dispose()
        }
        if (AnimationManager.getInstance().getSubscribe() != null) {
            AnimationManager.getInstance().getSubscribe().dispose()
        }

    }


    private var mActivity: Activity? = null
    override fun onAttach(context: Context) {
        super.onAttach(context)
        mActivity = activity
    }

    private fun saveUserBean() {
        if (mActivity == null) return
        val bean = (mActivity as PreMainActivity).getUserBean()
        AppSettingManager.saveObject(bean, "user_info")
        postUser(bean)
    }

    private fun postUser(bean: UserBean) {
        NetworkEngine.postUserInfo(
            bean.name, bean.gender, bean.birthday, bean.bornPlace,
            bean.marial_status, listener
        )
    }

    private val listener = object : NetworkListener<BaseBean>() {
        override fun onSuccess(t: BaseBean) {
            Logger.debug("LoadFragment", "onSuccess")
        }

        override fun onFailed(t: BaseBean) {
            super.onFailed(t)
            Logger.debug("LoadFragment", "onFailed")
        }

        override fun onError(e: String?) {
            super.onError(e)
            Logger.debug("LoadFragment", "onError")
        }
    }

    private fun initView(view: View) {
        mRoot = view.findViewById(R.id.linear_root_load)
        percent = view.findViewById(R.id.text_view_percent)
        process = view.findViewById(R.id.progress_bar_load)
    }

    fun fadeInRoot() {
        if (mRoot != null) {
            super.fadeInRoot(mRoot!!)
        }

    }

    @SuppressLint("SetTextI18n")
    private fun setGuideContent(textView: TextView, name: String, str: String) {
        if (!SysUtils.isContainChinese(name)) {
            textView.text = "$name! $str"
        } else {
            val byteSize = name.toByteArray().size
            val newByte = ByteArray(18)
            if (byteSize > 18) {
                for (i in 0..17) {
                    newByte[i] = name.toByteArray()[i]
                }
                textView.text = String(newByte) + "...! " + str
            } else {
                textView.text = "$name! $str"
            }
        }
    }
}